home *** CD-ROM | disk | FTP | other *** search
- Path: news.th-darmstadt.de!news
- From: Enno Sandner <enno@intellektik.informatik.th-darmstadt.de>
- Newsgroups: comp.lang.c++
- Subject: Re: Function overloading problem
- Date: Thu, 18 Jan 1996 08:57:32 +0100
- Organization: Fachbereich Informatik, TH Darmstadt
- Message-ID: <30FDFD6C.167EB0E7@intellektik.informatik.th-darmstadt.de>
- References: <DLBxoH.GLw@ariel.cs.yorku.ca>
- NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b5 (X11; I; SunOS 4.1.3 sun4m)
-
- IAN J COLOMBY wrote:
- >
- > I have the following code which in which the function call of f() inside
- > function g() doesn't behave the way I expected it to.
- >
- > class B;
- > class C;
- >
- > class A
- > {
- > virtual void f(B*);
- > virtual void f(C*);
- > };
- >
- > class B: public A
- > {
- > void f(B*);
- > void f(C*);
- > };
- >
- > class C: public A
- > {
- > void f(B*);
- > void f(C*);
- > };
- >
- > void g(A* a1, A* a2)
- > {
- > a1->f(a2);
- > }
- >
- > The above code first of all won't compile unless I insert the following
- > line of code into each class definition:
- > vitrual void f(A*) in class A or void f(A*) in classes B & C.
-
- What else do you expect ?
- A pointer to a derived class may be silently converted to a base-class
- pointer but not vice versa.
-
- >
- > The next problem is that the above line f(A*) is the function that always
- > gets called in the class of a1 no matter what a2 is in function g(). a1
- > and a2 will never be of class A, they will only be of either class B or
- > class C, so I'm not sure why the the call a1->f(a2) always calls the
- > function f(A*) in the class of a1, and not f(B*) or f(C*) depending on
- > what class a2 is.
- >
- > Any help in solving this problem would be appreciated.
- >
-
- The appropriate function is determined at compile-time, ie. the static-type
- is used to choose the function, which is in this case A.
- A reasonable solution to this problem seems to be the 'Visitor' pattern as
- described in the 'Design Patterns' book.
-
- Enno
-